package server

import (
	"context"
	"math/rand"
	"srv-server/basic/global"
	__ "srv-server/basic/proto"
	"srv-server/handler/model"
	"srv-server/handler/pkg"
	"time"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedSrvServer
}

func (s *Server) SendSms(_ context.Context, in *__.SendSmsRequest) (*__.SendSmsResponse, error) {

	get := global.Rdb.Get(context.Background(), "Send"+in.Mobile+in.Source)
	if get.Val() != "" {

		return &__.SendSmsResponse{
			Type: false,
			Msg:  "请勿在一分钟内重复发送",
		}, nil
	}
	code := rand.Intn(9000) + 1000

	_, err := global.Rdb.Set(context.Background(), "SendSms"+in.Mobile+in.Source, code, time.Minute*10).Result()
	if err != nil {
		return &__.SendSmsResponse{
			Type: false,
			Msg:  "验证码发送失败",
		}, nil
	}

	global.Rdb.Set(context.Background(), "Send"+in.Mobile+in.Source, 1, time.Minute*1)

	return &__.SendSmsResponse{
		Type: true,
		Msg:  "验证码发送成功",
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	var u model.VideoUser
	global.DB.Where("mobile = ?", in.Mobile).Find(&u)
	if len(in.Mobile) > 11 {
		return &__.LoginResponse{
			Code: 500,
			Msg:  "手机号格式错误",
		}, nil
	}
	if u.Id == 0 {

		u = model.VideoUser{
			Mobile: in.Mobile,
		}

		result, _ := global.Rdb.Get(context.Background(), "SendSms"+in.Mobile+"Login").Result()
		if result != in.SendSms {
			return &__.LoginResponse{
				Code: 500,
				Msg:  "注册短信验证码错误或已过期",
			}, nil
		}

		if err := global.DB.Create(&u).Error; err != nil {
			return &__.LoginResponse{
				Code: 500,
				Msg:  "注册失败",
			}, nil
		}
	} else {
		result, _ := global.Rdb.Get(context.Background(), "SendSms"+in.Mobile+"Login").Result()
		if result != in.SendSms {
			return &__.LoginResponse{
				Code: 500,
				Msg:  "登录短信验证码或已过期",
			}, nil
		}

	}

	return &__.LoginResponse{
		Code: 200,
		Msg:  "登录成功",
		Id:   int64(u.Id),
	}, nil
}
func (s *Server) HomePage(_ context.Context, in *__.HomePagRequest) (*__.HomePageResponse, error) {

	var u []model.VideoUser
	var home []*__.HomeList
	if in.UserId == 0 {
		global.DB.Model(model.VideoUser{}).Find(&u)
	} else {
		global.DB.Model(model.VideoUser{}).Where("id = ?", in.UserId).Find(&u)
	}
	if in.UserId == 0 {
		for _, user := range u {
			list := __.HomeList{

				Nickname:      user.Nickname,
				AccountNumber: user.AccountNumber,
				Constellation: user.Constellation,
				FolloInterest: user.FolloInterest,
				FansNumber:    user.FansNumber,
				GoodsNumber:   float32(user.GoodsNumber),
				WorksNumber:   user.WorksNumber,
			}
			home = append(home, &list)
		}
	} else {
		for _, user := range u {
			list := __.HomeList{
				Mobile:        user.Mobile,
				Nickname:      user.Nickname,
				AccountNumber: user.AccountNumber,
				Address:       user.Address,
				Constellation: user.Constellation,
				FolloInterest: user.FolloInterest,
				FansNumber:    user.FansNumber,
				GoodsNumber:   float32(user.GoodsNumber),
				WorksNumber:   user.WorksNumber,
				LikesNumber:   user.LikesNumber,
			}
			home = append(home, &list)
		}
	}

	return &__.HomePageResponse{
		List: home,
	}, nil
}
func (s *Server) PubWork(_ context.Context, in *__.PubWorkRequest) (*__.PubWorkResponse, error) {

	var music model.Music
	global.DB.Where("id = ?", in.WorksMusicId).Find(&music)
	if music.Id == 0 {

		return &__.PubWorkResponse{
			Code: 404,
			Msg:  "音乐不存在",
		}, nil
	}
	var subject model.Subjet
	global.DB.Where("id = ?", in.WorksSubjectId).Find(&subject)
	if subject.Id == 0 {
		return &__.PubWorkResponse{
			Code: 404,
			Msg:  "话题不存在",
		}, nil
	}

	var w model.Works
	w = model.Works{

		PubUserId:      int32(in.UserId),
		WorksTitle:     in.WorksTitle,
		WorksMusicId:   int32(music.Id),
		WorksImage:     in.WorksImage,
		WorksSubjectId: int32(subject.Id),
		WorksType:      int8(in.WorksType),
		WorksExamine:   in.WorksExamine,
	}
	if err := global.DB.Create(&w).Error; err != nil {
		return &__.PubWorkResponse{
			Code: 500,
			Msg:  "作品发布失败",
		}, nil
	}

	return &__.PubWorkResponse{
		Code: 200,
		Msg:  "作品发布成功",
	}, nil
}
func (s *Server) WorksExamine(_ context.Context, in *__.WorksExamineRequest) (*__.WorksExamineResponse, error) {

	var w model.Works

	global.DB.Where("id = ?", in.WorkId).Find(&w)

	w = model.Works{

		WorksExamine: in.WorksExamine,
	}
	if err := global.DB.Where("id = ?", in.WorkId).Updates(&w).Error; err != nil {
		return &__.WorksExamineResponse{
			Code: 500,
			Msg:  "作品审核失败",
		}, nil
	}

	return &__.WorksExamineResponse{
		Code: 200,
		Msg:  "作品审核成功",
	}, nil

}

func (s *Server) SubjectList(_ context.Context, in *__.SubjectListRequest) (*__.SubjectListResponse, error) {

	var w []model.Subjet
	var home []*__.SubjectList

	global.DB.Model(model.Subjet{}).Order("hot desc").Find(&w)

	for _, user := range w {
		list := __.SubjectList{
			Subject: user.Subject,
			Hot:     float32(user.Hot),
		}
		home = append(home, &list)

	}

	return &__.SubjectListResponse{
		List: home,
	}, nil

}

func (s *Server) ExamineComment(_ context.Context, in *__.ExamineCommentRequest) (*__.ExamineCommentResponse, error) {

	var c model.Comment

	if !pkg.ReviewText(in.Context) {
		return &__.ExamineCommentResponse{
			Code: 500,
			Msg:  "内容违规，无法评论",
		}, nil
	}
	c = model.Comment{

		Context: in.Context,
		Userid:  int32(in.Userid),
		PadId:   int32(in.PadId),
	}
	if err := global.DB.Create(&c).Error; err != nil {
		return &__.ExamineCommentResponse{
			Code: 500,
			Msg:  "评论失败",
		}, nil
	}
	return &__.ExamineCommentResponse{
		Code: 500,
		Msg:  "内容违规，无法评论",
	}, nil
}
